package com.lifeng.controller;

import com.lifeng.bean.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

/**
 * Created by lifeng on 2019/10/16.
 * 参考
 * https://blog.csdn.net/qq_36911145/article/details/82854417
 */
@RestController
@RequestMapping("/user")
@Api(value="用户接口",tags = "用户接口",description = "用户接口详细信息描述")
public class UserController {

    @RequestMapping(value = "/{userId}",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="查询",notes="根据userId查询用户信息",response=User.class)
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    public User findById(@RequestParam("userId") String userId){
        User user = new User();
        user.setId(userId);
        user.setName("张三");
        user.setAge(22);
        Instant instant = LocalDate.of(1992,8,12).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        user.setBithday(date);
        return user;
    }

    @RequestMapping(value = "/delete/{userId}",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="删除",notes="根据userId删除用户",response=String.class)
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    public String deleteById(@RequestParam("userId") String userId){
        return "ok";
    }

    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    @ApiOperation(value="添加用户",notes="添加用户",response=User.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名称(中文名称3到5位)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "age", value = "用户年龄", required = true, dataType = "int"),
            @ApiImplicitParam(name = "bithday", value = "用户生日(格式如2019/12/23)", required = false, dataType = "String")
    })
    public User add(String name,Integer age,Date bithday){
        User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setName(name);
        user.setAge(age);
        user.setBithday(bithday);
        return user;
    }
}
